//
//  HomepwnerItemCell.m
//  Homepwner
//
//  Created by jiayi xiao on 12-4-30.
//  Copyright 2012年 nju. All rights reserved.
//

#import "HomepwnerItemCell.h"
#import "Possession.h"

@implementation HomepwnerItemCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
        valueLabel=[[UILabel alloc]initWithFrame:CGRectZero];
        [[self contentView] addSubview:valueLabel];
        [valueLabel release];
        
        nameLabel=[[UILabel alloc]initWithFrame:CGRectZero];
        [[self contentView] addSubview:nameLabel];
        [nameLabel release];
        
        imageView=[[UIImageView alloc]initWithFrame:CGRectZero];
        [[self contentView] addSubview:imageView];
        [imageView setContentMode:UIViewContentModeCenter];
        [imageView release];
        
        serialLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [[self contentView] addSubview:serialLabel];
        [serialLabel release];
        
        creationLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [[self contentView] addSubview:creationLabel];
        [creationLabel release];
        
        [self setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
        contentToggle = NO;
    }
    return self;
}

-(void) layoutSubviews
{
    [super layoutSubviews];
    
    float inset=5.0;
    
    CGRect bounds=[[self contentView]bounds];
    
    float h=bounds.size.height;
    float w=bounds.size.width;
    
    float valueWidth=40.0;
    float creationWidth = 200.0;
    
    CGSize thumbnailSize= [Possession thumbnailSize];
    float imageSpace=h-thumbnailSize.height;
    
    CGRect imageFrame=CGRectMake(inset, imageSpace/2.0, thumbnailSize.width, thumbnailSize.height);
    [imageView setFrame:imageFrame];
        
//    CGRect nameFrame=CGRectMake(imageFrame.origin.x+imageFrame.size.width+inset, inset, w-(4*inset+valueWidth+40), h-inset*2.0);
//    [nameLabel setFrame:nameFrame];
//    
//    CGRect valueFrame=CGRectMake(nameFrame.origin.x+nameFrame.size.width+inset, inset, valueWidth, h-inset*2.0);
//    [valueLabel setFrame:valueFrame];
    
    
    if (!contentToggle) {
        // Create a rectangle in the middle for the name
        [serialLabel setFrame:CGRectZero];
        [creationLabel setFrame:CGRectZero];
        CGRect nameFrame = CGRectMake((imageFrame.size.width + imageFrame.origin.x + inset), 
                                      inset, 
                                      w - (h+ valueWidth + inset * 4.0), 
                                      h - inset * 2.0);
        [nameLabel setFrame:nameFrame];
        
        // Create a rectangle on the right side of the cell for the value
        CGRect valueFrame = CGRectMake((nameFrame.size.width + nameFrame.origin.x + inset), 
                                       inset, valueWidth, h - inset * 2.0);
        [valueLabel setFrame:valueFrame];
    } else {
        [nameLabel setFrame:CGRectZero];
        [valueLabel setFrame:CGRectZero];
        // Create a rectangle in the middle for the serial number
        CGRect serialFrame = CGRectMake((imageFrame.size.width + imageFrame.origin.x + inset), 
                                        inset, 
                                        w - (h+ creationWidth + inset * 4.0), 
                                        h - inset * 2.0);
        [serialLabel setFrame:serialFrame];
        
        // Create a rectangle on the right side of the cell for the creation date
        CGRect creationFrame = CGRectMake((serialFrame.size.width + serialFrame.origin.x + inset), 
                                          inset, creationWidth, h - inset * 2.0);
        [creationLabel setFrame:creationFrame];
    }
}

-(void) setPossession:(Possession *)possession
{
    [valueLabel setText:[NSString stringWithFormat:@"$%d",[possession valueInDollars]]];
    [nameLabel setText:[possession possessionName]];
    NSDateFormatter  *dateFormatter=[[[NSDateFormatter alloc]init]autorelease];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [creationLabel setText:[dateFormatter stringFromDate:[possession dateCreated]]];
    [serialLabel setText:[possession serialNumber]];
    [imageView setImage:[possession thumbnail]];
}

- (void)toggleContent {
    contentToggle = (contentToggle ? NO : YES);
    [self setNeedsLayout];
}

@end
